define(['jquery'],  function ($) {

/* ---------- Flex Box 补丁  v0.2 ---------- */

    var CSS_Attribute = {
            Float:        $.makeSet('absolute', 'fixed'),
            Display:      $.makeSet('block', 'table'),
            Prefix:       (function (iUA) {
                try {
                    return ({
                        webkit:     '-webkit-',
                        gecko:      '-moz-',
                        trident:    '-ms-'
                    })[
                        iUA.match( /Gecko|WebKit|Trident/i )[0].toLowerCase()
                    ];
                } catch (iError) {  return '';  }

            })( navigator.userAgent ),
            Flex_Size:    {
                horizontal:    {
                    length:    'width',
                    margin:    ['left', 'right']
                },
                vertical:      {
                    length:    'height',
                    margin:    ['top', 'bottom']
                }
            }
        };

    function FlexFix() {

        var $_Box = $( this );

        var Size_Name = CSS_Attribute.Flex_Size[
                $_Box.css(CSS_Attribute.Prefix + 'box-orient')
            ];

        var Flex_Child = $.extend([ ], {
                sum:       0,
                sum_PX:    (
                    $_Box[ Size_Name.length ]()  -
                    parseFloat( $_Box.css('padding-' + Size_Name.margin[0]) )  -
                    parseFloat( $_Box.css('padding-' + Size_Name.margin[1]) )
                )
            });

        $_Box.children().each(function () {

            var $_This = $( this );

            if (
                ($_This.css('position') in CSS_Attribute.Float) ||
                ($_This.css('display') === 'none')
            )
                return;

            var iDisplay = $_This.css('display').match(
                    RegExp(
                        '(' + CSS_Attribute.Prefix + ')?(inline)?-?(.+)?$',  'i'
                    )
                );

            if ( iDisplay[2] )
                $_This.css({
                    display:    iDisplay[3] ?
                        (
                            (
                                (iDisplay[3] in CSS_Attribute.Display)  ?
                                    ''  :  CSS_Attribute.Prefix
                            ) + iDisplay[3]
                        ) :
                        'block'
                });

            var _Index_ = Flex_Child.push( {$_DOM: $_This} )  -  1,
                _Length_ = $_This[ Size_Name.length ]();

            if (! _Length_)  return Flex_Child.pop();

            Flex_Child[_Index_].scale = parseInt(
                $_This.css(CSS_Attribute.Prefix + 'box-flex')
            );

            Flex_Child.sum += Flex_Child[_Index_].scale;

            Flex_Child.sum_PX -= (
                _Length_ +
                parseFloat(
                    $_This.css('margin-' + Size_Name.margin[0])
                ) +
                parseFloat(
                    $_This.css('margin-' + Size_Name.margin[1])
                )
            );
        });

        if (Flex_Child.sum_PX < 0)  Flex_Child.sum_PX = 0;

        var iUnit = Flex_Child.sum_PX / Flex_Child.sum;

        for (var i = 0;  Flex_Child[i];  i++)
            Flex_Child[i].$_DOM[ Size_Name.length ](
                Flex_Child[i].$_DOM[ Size_Name.length ]()  +
                    (Flex_Child[i].scale * iUnit)
            );
    }

    var _addClass_ = $.fn.addClass;

    if ('flex' in document.documentElement.style)  return;

    $.fn.addClass = function () {

        _addClass_.apply(this, arguments);

        if ($.inArray('Flex-Box', arguments[0].split(/\s+/))  >  -1)
            return  this.each( FlexFix );

        return this;
    };

    $( document ).ready(function () {

        $('.Flex-Box').each( FlexFix );
    });

});